package com.inspur.security.cbb3.kms.keyusage.dataorders.controller;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.inspur.security.cbb3.kms.annotation.SysLog;
import com.inspur.security.cbb3.kms.enums.BusinessType;
import com.inspur.security.cbb3.kms.enums.SystemCode;
import com.inspur.security.cbb3.kms.keymanager.keyorders.po.KeyOrdersBean;
import com.inspur.security.cbb3.kms.keymanager.keyorders.vo.KeyOrdersAddVO;
import com.inspur.security.cbb3.kms.keyusage.data.po.DataBean;
import com.inspur.security.cbb3.kms.keyusage.dataorders.po.DataOrdersBean;
import com.inspur.security.cbb3.kms.keyusage.dataorders.service.DataOrdersService;
import com.inspur.security.cbb3.kms.keyusage.dataorders.vo.DataOrdersAddVO;
import com.inspur.security.cbb3.kms.syslog.service.SysLogService;
import com.inspur.security.cbb3.kms.utils.PageRequestDate;
import com.inspur.security.cbb3.kms.utils.Result;
import com.inspur.security.cbb3.kms.utils.ResultUtil;
import javassist.NotFoundException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.*;

import javax.validation.ValidationException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * DataordersController
 *
 * @author liukai
 * @date 2020/10/15
 **/

@RestController
@RequestMapping("/v1/dataorders")
public class DataOrdersController {

    @Autowired
    DataOrdersService dataOrdersService;

    @Autowired
    SysLogService sysLogService;

    @PostMapping
    @SysLog(title = "数据订单新增", businessType = BusinessType.INSERT)
    public ResponseEntity addDataOrder(DataOrdersBean dataOrdersBean, DataBean dataBean) {
        DataOrdersBean bean = dataOrdersService.addDataOrder(dataOrdersBean, dataBean);
        //调研订单处理接口
        dataOrdersService.sendOrder(bean.getUuid());
        Map<String,String> returnMap=new HashMap<>(1);
        returnMap.put("dataorderRef","/v1/dataorders/"+bean.getUuid());
        return ResultUtil.objReturn(returnMap, HttpStatus.ACCEPTED);
    }

    @GetMapping
    public ResponseEntity searchDataOrders(PageRequestDate pageRequestDate, DataOrdersAddVO searchParams) {
        Page<DataOrdersBean> page = dataOrdersService.searchDataOrders(pageRequestDate, searchParams);
        //封装返回值
        Map<String,Object> returnMap=new HashMap<>(2);
        returnMap.put("total",page.getTotalElements());
        if(page.getTotalElements()<1){
            returnMap.put("dataorders",null);
            return ResultUtil.objReturn(returnMap, HttpStatus.OK);
        }
        List<DataOrdersBean> list= page.getContent();
        for(DataOrdersBean bean:list){
            bean.setDataRef("/v1/data/"+bean.getDataId()+"/resultdata");
        }
        returnMap.put("dataorders",list);
        return ResultUtil.objReturn(returnMap, HttpStatus.OK);
    }

    @GetMapping("/{uuid}")
    public ResponseEntity searchDataOrder(@PathVariable(name = "uuid") String uuid) throws NotFoundException, MissingServletRequestParameterException {
        DataOrdersBean bean = dataOrdersService.searchDataOrder(uuid);
        bean.setDataRef("/v1/data/"+bean.getDataId()+"/resultdata");
        return ResultUtil.objReturn(bean, HttpStatus.OK);
    }

    @DeleteMapping("/{uuid}")
    @SysLog(title = "删除数据订单", businessType = BusinessType.DELETE)
    public ResponseEntity deleteDataOrder(@PathVariable(name = "uuid") String uuid) throws NotFoundException, MissingServletRequestParameterException {
        dataOrdersService.deleteDataOrder(uuid);
        return ResultUtil.noDataReturn(SystemCode.SUCCESS, HttpStatus.NO_CONTENT);
    }
}
